package com.zhihuichongcheng.application.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.smartlib.cmnObject.util.SPUtil;
import com.umeng.analytics.MobclickAgent;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.BaseApplication;

/**
 * @author hewenwen
 * @description <在此写用途>
 * @date 2016/4/24
 */
public class BaseFragment extends Fragment {
    protected BaseApplication app;
    protected Context mContext;
    protected UserBean mUser;
    protected HttpManager manager;
    protected View mView;
    protected SPUtil spUtil;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        spUtil = SPUtil.getInstance(getActivity());
        mContext = getActivity();

        app = (BaseApplication) getActivity().getApplication();
        mUser = app.getUser();
        mView = inflater.inflate(getLayoutId(), container, false);
        manager = HttpManager.getInstance(getActivity());
        init();

        return mView;
    }

    protected int getLayoutId() {
        return 0;
    }

    protected void init() {

    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(mContext);
        MobclickAgent.onPageEnd("base");
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(mContext);
        MobclickAgent.onPageStart("base");
    }

    protected void setTitle(String title) {
        TextView tvTitle = (TextView) mView.findViewById(R.id.navbar_title);
        if (tvTitle != null) {
            tvTitle.setText(title);
        }
    }

    protected void startActivity(Class cls, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void startActivityByIntent(Intent intent) {
        startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void startActivityForResult(int requestCode, Class cls, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }

    protected void hideKeyboard(EditText edit) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
    }
}
